<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use App\Repositories\RoleRepository;

/**
 * Class RolesController.
 *
 * @package namespace App\Http\Controllers\Admin;
 */
class RolesController extends Controller
{
    /**
     * @var RoleRepository
     */
    protected $repository;

    /**
     * RolesController constructor.
     * @param RoleRepository $repository
     */
    public function __construct(RoleRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->repository->pushCriteria(new \App\Criteria\SearchableCriteria($request));
        $roles = $this->repository->paginate(request('limit'))->toArray();
        if (request()->wantsJson()) {
            return response()->json([
                'code' => 0,
                'msg' => '',
                'count' => $roles['total'],
                'data' => $roles['data'],
            ]);
        }
        return view('admin.admin.roles.index', compact('roles'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function create(Request $request, $id = 0)
    {
        if (request()->wantsJson()) {
            $data = $request->only('name', 'remark');
            $role = $this->repository->create($data);
            if (!$role) {
                return response()->json([
                    'code' => 1001,
                    'msg' => '操作失败'
                ]);
            }
            return response()->json([
                'code' => 0,
                'msg' => '操作成功'
            ]);
        }
        return view('admin.admin.roles.form');
    }

    /**
     * Update a created resource in storage.
     */
    public function update(Request $request, $id = 0)
    {
        if (request()->wantsJson()) {
            $data = $request->only('name', 'remark');
            $role = $this->repository->update($data, $id);
            if (!$role) {
                return response()->json([
                    'code' => 1001,
                    'msg' => '操作失败'
                ]);
            }
            return response()->json([
                'code' => 0,
                'msg' => '操作成功'
            ]);
        }
        $role = $id > 0 ? $this->repository->find($id) : null;
        return view('admin.admin.roles.form', compact('role'));
    }

    /**
     * 分配权限
     */
    public function relate(Request $request, $id)
    {
        if (request()->wantsJson()) {
            if (!$this->repository->relate($request->all(), $id)) {
                return response()->json([
                    'code' => 1001,
                    'msg' => '操作失败'
                ]);
            }
            return response()->json([
                'code' => 0,
                'msg' => '操作成功'
            ]);
        }
        $related = $this->repository->related($id);
        return view('admin.admin.roles.relate', compact('related'));
    }

    /**
     * 启用
     */
    public function enable(Request $request)
    {
        if (!$this->repository->update(['is_disabled' => 0], $request->input('id'))) {
            return response()->json([
                'code' => 1001,
                'msg' => '操作失败'
            ]);
        }
        return response()->json([
            'code' => 0,
            'msg' => '操作成功'
        ]);
    }

    /**
     * 禁用
     */
    public function disable(Request $request)
    {
        if (!$this->repository->update(['is_disabled' => 1], $request->input('id'))) {
            return response()->json([
                'code' => 1001,
                'msg' => '操作失败'
            ]);
        }
        return response()->json([
            'code' => 0,
            'msg' => '操作成功'
        ]);
    }
}
